﻿Imports MySql.Data.MySqlClient
Imports System.IO
Imports System.Data
Public Class showhelpdesk
    Dim sql As MySqlConnection
    Dim idkey As String
    Dim ipconnect As String
    Dim usernamedb As String
    Dim dbname As String
    Dim mysqlpass As String
    Dim id_user As String
    Dim position_user As String
    Public Sub New(ByRef mysql_pass As String, ByRef ip_connect As String, ByRef user_namedb As String, ByRef db_name As String)
        InitializeComponent()
        mysqlpass = mysql_pass
        ipconnect = ip_connect
        usernamedb = user_namedb
        dbname = db_name
    End Sub
    Private Sub showhelpdesk_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        sql = New MySqlConnection
        sql.ConnectionString = "server=" + ipconnect + ";user id=" + usernamedb + ";password=" + mysqlpass + ";database=" + dbname + ";Character Set =utf8;"
        Try
            sql.Open()
        Catch ex As Exception
            MsgBox("Can't Connect to database" + ex.Message)
            Me.Close()
        End Try
        Dim mysqlcommand As New MySqlCommand
        Dim mysqladeptor As New MySqlDataAdapter
        Dim mysqlreader As MySqlDataReader
        Dim key As String
        Dim count As Integer

        count = 0
        key = ComboBoxsearchname.Text

        If sql.State = ConnectionState.Closed Then
            sql.Open()
        End If

        mysqlcommand.CommandText = " SELECT * FROM helpdesk order by idhelpdesk"
        mysqlcommand.Connection = sql
        mysqladeptor.SelectCommand = mysqlcommand
        Try
            mysqlreader = mysqlcommand.ExecuteReader
            ListViewshow.Items.Clear()
            While (mysqlreader.Read())
                With ListViewshow.Items.Add(mysqlreader("idhelpdesk"))
                    .subitems.add(mysqlreader("problem"))
                    .subitems.add(mysqlreader("edit_problem"))
                    .subitems.add(mysqlreader("name_person"))
                    .subitems.add(mysqlreader("state_problem"))
                    .subitems.add(mysqlreader("officer"))
                    .subitems.add(mysqlreader("date"))
                    .subitems.add(mysqlreader("agencies"))
                End With
            End While
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        sql.Close()
    End Sub

    Private Sub showdatashow()
        Dim mySqlCommand As New MySqlCommand
        Dim mySqlAdaptor As New MySqlDataAdapter
        Dim mySqlReader As MySqlDataReader
        Dim key As String
        Dim count As Integer

        count = 0
        key = ComboBoxsearchname.Text

        If sql.State = ConnectionState.Closed Then
            sql.Open()
        End If

        mySqlCommand.CommandText = " SELECT * FROM helpdesk where officer like '%" + key + "%' and agencies like '%" + ComboBoxsearchagen.Text + "%' and type_problem like '%" + ComboBoxsearchtype.Text + "%' order by idhelpdesk ;"
        mySqlCommand.Connection = sql
        mySqlAdaptor.SelectCommand = mySqlCommand

        Try
            mySqlReader = mySqlCommand.ExecuteReader
            ListViewshow.Items.Clear()
            While (mySqlReader.Read())
                With ListViewshow.Items.Add(mySqlReader("idhelpdesk"))
                    .subitems.add(mySqlReader("problem"))
                    .subitems.add(mySqlReader("edit_problem"))
                    .subitems.add(mySqlReader("name_person"))
                    .subitems.add(mySqlReader("state_problem"))
                    .subitems.add(mySqlReader("officer"))
                    .subitems.add(mySqlReader("date"))
                    .subitems.add(mySqlReader("agencies"))

                End With
            End While
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        sql.Close()
    End Sub


    Private Sub btnback_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnback.Click
        Dim NextForm As helpdesk = New helpdesk(mysqlpass, ipconnect, usernamedb, dbname)
        NextForm.Show()
        Me.Hide()
    End Sub

    Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click
        showdatashow()
        ComboBoxsearchagen.Text = ""
        ComboBoxsearchname.Text = ""
        ComboBoxsearchtype.Text = ""

    End Sub

    Private Sub ComboBoxsearchname_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxsearchname.SelectedIndexChanged
        showdatashow()
    End Sub

    Private Sub ComboBoxsearchagen_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxsearchagen.SelectedIndexChanged
        showdatashow()
    End Sub

    Private Sub ComboBoxsearchtype_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxsearchtype.SelectedIndexChanged
        showdatashow()
    End Sub

    Private Sub btnedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnedit.Click
        Dim NextForm As edithelpdesk = New edithelpdesk(mysqlpass, ipconnect, usernamedb, dbname)
        NextForm.Show()
        Me.Hide()
    End Sub
End Class